{% extends "base.html" %}

{% block title %}新手推荐 - 股票推荐系统{% endblock %}

{% block content %}
<div class="main-container">
    <a href="/" class="btn btn-outline-light back-btn">
        <i class="fas fa-arrow-left"></i> 返回首页
    </a>
    
    <div class="header">
        <h1><i class="fas fa-seedling"></i> 新手稳健推荐</h1>
        <p>为新手投资者提供低风险、高流动性的股票组合建议</p>
    </div>
    
    <div class="row">
        <div class="col-md-4">
            <div class="card card-custom">
                <div class="card-body">
                    <h5 class="card-title"><i class="fas fa-cog"></i> 推荐参数设置</h5>
                    
                    <form id="noviceForm">
                        <div class="mb-3">
                            <label for="top_n" class="form-label">推荐股票数量</label>
                            <input type="number" class="form-control form-control-custom" id="top_n" value="5" min="1" max="20">
                        </div>
                        
                        <div class="mb-3">
                            <label for="vol_threshold" class="form-label">波动率阈值</label>
                            <input type="number" class="form-control form-control-custom" id="vol_threshold" value="4.87" step="0.01" min="0">
                            <small class="text-muted">控制风险，值越小风险越低</small>
                        </div>
                        
                        <div class="mb-3">
                            <label for="turnover_min" class="form-label">最小成交额（亿元）</label>
                            <input type="number" class="form-control form-control-custom" id="turnover_min" value="7.22" step="0.01" min="0">
                            <small class="text-muted">确保流动性，值越大流动性越好</small>
                        </div>
                        
                        <div class="mb-3">
                            <label for="max_per_industry" class="form-label">每行业最大股票数</label>
                            <input type="number" class="form-control form-control-custom" id="max_per_industry" value="1" min="1" max="5">
                            <small class="text-muted">控制行业分散度</small>
                        </div>
                        
                        <button type="submit" class="btn btn-custom btn-success w-100">
                            <i class="fas fa-magic"></i> 开始推荐
                        </button>
                    </form>
                </div>
            </div>
            
            <div class="card card-custom mt-3">
                <div class="card-body">
                    <h6 class="card-title"><i class="fas fa-info-circle"></i> 推荐策略说明</h6>
                    <ul class="list-unstyled">
                        <li><i class="fas fa-check text-success"></i> 筛选低波动率股票</li>
                        <li><i class="fas fa-check text-success"></i> 确保高成交额流动性</li>
                        <li><i class="fas fa-check text-success"></i> 控制行业分散度</li>
                        <li><i class="fas fa-check text-success"></i> 适合新手稳健投资</li>
                    </ul>
                </div>
            </div>
        </div>
        
        <div class="col-md-8">
            <div class="card card-custom">
                <div class="card-body">
                    <h5 class="card-title"><i class="fas fa-list"></i> 推荐结果</h5>
                    
                    <div class="loading" id="loading" style="display: none;">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">加载中...</span>
                        </div>
                        <p class="mt-2">正在为您分析股票数据...</p>
                    </div>
                    
                    <div id="resultContainer" style="display: none;">
                        <div class="alert alert-info" id="resultMessage"></div>
                        
                        <div class="table-responsive">
                            <table class="table table-hover result-table">
                                <thead>
                                    <tr>
                                        <th>股票代码</th>
                                        <th>股票名称</th>
                                        <th>所属行业</th>
                                        <th>波动率</th>
                                        <th>平均成交额（亿元）</th>
                                    </tr>
                                </thead>
                                <tbody id="resultTable">
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- 可视化图表区域 -->
                        <div id="chartContainer" class="mt-4" style="display: none;">
                            <h6><i class="fas fa-chart-bar"></i> 可视化分析</h6>
                            <div class="text-center">
                                <div id="chartSvg" style="max-width: 100%; height: auto;"></div>
                            </div>
                        </div>
                    </div>
                    
                    <div id="noResult" style="display: none;">
                        <div class="text-center py-5">
                            <i class="fas fa-search fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">点击"开始推荐"获取股票建议</h5>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    $('#noviceForm').on('submit', function(e) {
        e.preventDefault();
        
        // 显示加载状态
        showLoading('#loading');
        $('#resultContainer').hide();
        $('#noResult').hide();
        
        // 获取表单数据
        const formData = {
            top_n: parseInt($('#top_n').val()),
            vol_threshold: parseFloat($('#vol_threshold').val()),
            turnover_min: parseFloat($('#turnover_min').val()),
            max_per_industry: parseInt($('#max_per_industry').val())
        };
        
        // 发送请求
        $.ajax({
            url: '/api/novice_recommend',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(formData),
            success: function(response) {
                hideLoading('#loading');
                
                if (response.success) {
                    // 显示结果
                    $('#resultMessage').text(response.message);
                    $('#resultTable').html(formatTableData(response.data));
                    $('#resultContainer').show();
                    $('#noResult').hide();
                    
                    // 显示可视化图表
                    if (response.chart) {
                        $('#chartSvg').html(response.chart);
                        $('#chartContainer').show();
                    } else {
                        $('#chartContainer').hide();
                    }
                } else {
                    showAlert(response.error, 'danger');
                    $('#noResult').show();
                }
            },
            error: function(xhr, status, error) {
                hideLoading('#loading');
                showAlert('网络错误，请稍后重试', 'danger');
                $('#noResult').show();
            }
        });
    });
    
    // 页面加载时显示初始状态
    $('#noResult').show();
});
</script>
{% endblock %}
